<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/19 16:34
 * Desc: 组合产品
 */
trait Trait_Api_Combination
{
    /**
     * 获取组合产品套餐
     * @param $id
     * @return array
     */
    public function get_suit($id)
    {
        $date = strtotime(date('Ymd', time()));

        $product_list = DB::select('a.id', 'a.combination_id', 'a.product_id', 'a.typeid', 'a.suit_name', 'a.suit_describe', 'a.suit_id')
            ->from(array('combination_product', 'a'))
            ->join(array('combination_product_price', 'b'), 'left')
            ->on('a.id', '=', DB::expr('sline_b.combination_product_id'))
            ->where('a.ishidden', '=', 0)
            ->and_where('a.combination_id', '=', $id)
            ->and_where('b.day', '>=', $date)
            ->group_by('a.id')
            ->execute()
            ->as_array();

        $suit_status = true;
        foreach ($product_list as &$v) {
            if ($v['typeid'] == 114) {
                $v['outdoor'] = Model_Api_V2_Combination::get_outdoor($v);
            }

            $v['piclist']   = Model_Api_V2_Combination::get_piclist($v);
            $v['suit_list'] = Model_Api_V2_Combination::get_suit($v);
            $v['status']    = true;

            // 只要有产品没有选择套餐整个产品都不可以预订
            if (!$v['suit_list']) {
                $v['status'] = false;
                $suit_status = false;
            }
        }

        return array('product_list' => $product_list, 'suit_status' => $suit_status);
    }

    /**
     * 获取公告
     * @return array
     */
    public function get_notice()
    {
        $config                                  = Model_Sysconfig::get_configs(0, array('cfg_combination_notice_isopen', 'cfg_combination_notice_content'), 0);
        $config['cfg_combination_notice_isopen'] = empty($config['cfg_combination_notice_content']) ? 2 : $config['cfg_combination_notice_isopen'];

        return $config;
    }

    /**
     * 价格日历
     * @param $params
     * @return array
     * @throws Exception
     */
    public function calendar_price($params)
    {
        /**
         * @var $suit_id
         * @var $type_id
         * @var $combination_product_id
         */
        extract($params);

        $info = Model_Api_V2_Combination::calendar_price($combination_product_id, $suit_id, $type_id);
        if (!$info) {
            throw new Exception('获取失败');
        }

        return $info;
    }

    /**
     * 订单详情
     * @param $mid
     * @param $id
     * @return array|mixed
     * @throws Exception
     */
    public function order_detail($mid, $id)
    {
        $info = DB::select('ordersn')
            ->from('member_order')
            ->where('id', '=', $id)
            ->and_where('memberid', '=', $mid)
            ->execute()
            ->current();
        if (!$info) {
            throw new Exception('订单不存在');
        }

        $info = Model_Member_Order::order_info($info['ordersn'], null, 1);

        $info['dingnum']     = intval($info['dingnum']);
        $info['singleJifen'] = intval(intval($info['needjifen']) / intval($info['dingnum']));
        $info['litpic']      = $this->img_url($info['litpic']);
        $info['add_time']    = date('Y-m-d H:i:s', $info['addtime']);
        $info['remark']      = unserialize($info['remark'])['book'];
        $info['tourers']     = Model_Member_Order_Tourer::get_tourer_by_orderid($info['id']);/*游客信息*/
        $web_url             = DB::select()->from('weblist')->where('id=1')->execute()->get('weburl');
        $info['web_url']     = $web_url;

        //获得合同信息
        if (!empty($info['contract_id'])) {
            $contract                 = Model_Api_V2_Contract::get_order_contract($info['id']);
            $info['contract_content'] = $contract['status'] ? $contract['data'] : false;
        }

        //收益信息
        $income = Model_Member_Order_Compute::get_income_info($info['id']);
        if (St_Functions::is_normal_app_install('mobiledistribution') && method_exists('Model_Fenxiao', 'get_order_fenxiao_info')) {
            $fenxiao_info              = Model_Fenxiao::get_order_fenxiao_info($info);
            $income['platform_income'] = $income['platform_income'] - floatval($fenxiao_info['commission']) - floatval($fenxiao_info['commission1']) - floatval($fenxiao_info['commission2']) - floatval($fenxiao_info['commission3']);
        }
        if (!empty($income)) {
            $income['platform_commission'] = floatval($income['platform_commission']);
            $income['supplier_income'] = floatval($income['supplier_income']);
            $info['income'] = $income;
        }
        if (St_Functions::is_normal_app_install('coupon')) {
            $info['iscoupon'] = Model_Coupon::order_view($info['ordersn']);
            $info['cmoney']   = $info['iscoupon']['cmoney'];
        }
        /*子订单信息*/
        $info['current_verified_num'] = 0;
        $info['sub_order']            = array();
        $sub_order                    = DB::select('ordersn')
            ->from("member_order")
            ->where("pid","=","{$id}")
            ->execute()
            ->as_array();
        $info['refund_status']        = true;
        foreach ($sub_order as $v) {
            $sub_order_info = Model_Member_Order::order_info($v['ordersn'], null, 1);
            if ($sub_order_info['status'] == 5) {
                $info['refund_status'] = false;
            }
            $num = DB::select()
                ->from('member_order_verify')
                ->where("orderid","=","{$sub_order_info['id']}")
                ->execute()
                ->current();
            if ($num) {
                $info['current_verified_num'] += 1;
            }
            $sub_order_info['web_url'] = $web_url;
            $sub_order_info['dingnum'] = intval($sub_order_info['dingnum']);
            $sub_order_info['litpic']  = $this->img_url($sub_order_info['litpic']);
            $sub_order_info['addtime'] = date('Y-m-d H:i:s', $sub_order_info['addtime']);
            if ($sub_order_info['typeid'] == 114) {
                $sub_order_info['outdoor'] = Model_Api_V2_Combination::get_outdoor(array('product_id' => $sub_order_info['productautoid']));
            }
            if (St_Functions::is_normal_app_install('system_docs')) {
                $docs = DB::select()->from('member_order_docs')->where("ordersn", '=', $sub_order_info['ordersn'])->execute()->as_array();
                if ($docs) {
                    foreach ($docs as &$do) {
                        $do['ticket_num']  = $do['UUcode'];
                        $do['ticket_guid'] = $do['UUcode'];
                        $do['status']      = 2;
                    }
                    $sub_order_info["stccs_tickets"] = $docs;
                }
            }
            $sub_order_info['combination_product_name'] = DB::select()
                ->from('combination_product')
                ->where("combination_id={$info['productautoid']} and product_id={$sub_order_info['productautoid']} AND FIND_IN_SET({$sub_order_info['suitid']},suit_id) AND typeid={$sub_order_info['typeid']}")
                ->execute()
                ->get('suit_name');

            //验单部分暂时有bug
            if(!empty($sub_order_info['eticketno'])) {
                $whereStr = " (a.status='5' OR a.status='2') AND (a.isconsume='1' or b.id is not null)";
                $whereStr .= " AND a.eticketno='{$sub_order_info['eticketno']}'" ;
                $csql = "select b.dingnum as verified_dingnum,b.childnum as verified_childnum,b.oldnum as verified_oldnum,b.addtime as verified_addtime from sline_member_order a left join sline_member_order_verify b on a.id=b.orderid ";
                $csql .=" where {$whereStr} ";
                St_Functions::writeLog($csql,__LINE__);
                //返回当前页的数据结果
                $list =  DB::query(Database::SELECT,$csql)->execute()->as_array();

                $sub_order_info['current_verified_num']=0;
                foreach ($list as $key=>$vel) {
                    $sub_order_info['current_verified_num']+=(intval($vel['verified_dingnum'])+intval($vel['verified_childnum'])+intval($vel['verified_oldnum']));
                }
                $sub_order_info['eticketno_detail']=$list;
            }else{
                $sub_order_info['current_verified_num'] = 0;
            }

            unset($sub_order_info['memberid']);
            $info['sub_order'][]                        = $sub_order_info;
        }
        unset($info['memberid']);
        return $info;
    }
}